// import log from '@dfeidao/atom-nodejs/logger/log';
import operate from '@dfeidao/atom-nodejs/db/operate';
import parse from '@dfeidao/atom-nodejs/dt/parse';
import doc_code from '@dfeidao/atom-nodejs/msg/doc-code';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	plan_name: string;
	plan_describe: string;
	plan_type_no: string;
	plan_type_name: string;
	plan_cla_attr_no: string;
	plan_cla_attr_name: string;
	start_date: string;
	end_date: string;
	call_period: number;
	plan_implement_status: number;
	take_effect: number;
	plan_location: string;
	plan_mark: number;
	plan_cycle: string;
	request_date: string;

	plan_list: { plan_list_no: string; plan_list_name: string; };
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	// log('Service begin path:c:\Users\32645\Desktop\kh\src\plan-cb\zj-000002\s003,action_id:' + action_id);

	// log('Service end path:c:\Users\32645\Desktop\kh\src\plan-cb\zj-000002\s003,action_id:' + action_id);

	// _id	id
	// plan_type_no	计划类型编号1
	// plan_name	计划名称1
	// plan_describe	计划描述1
	// plan_cycle	计划周期1
	// plan_type_name	计划类型名称1
	// plan_cla_attr_no	计划分类属性编号1
	// plan_cla_attr_name	计划分类属性名称1
	// start_date	开始日期1
	// end_date	结束日期1
	// call_period	调用期1
	// plan_mark	计划标识1
	// plan_list_no	计划清单编号1
	// take_effect	是否生效1
	// plan_list_name	计划清单名称1
	// plan_implement_status	计划实施状态1
	// plan_location	计划地点1
	// request_date	关键日期1
	// plan_no	计划编号1
	// productid	产品ID


	const res = await doc_code(action_id, session_id, systemid, 'BM0015859', '', 1);
	const start_date = message.start_date;
	const s = parse(start_date);
	const end_date = message.end_date;
	const e = parse(end_date);


	const { exec, insert } = operate(action_id, session_id, systemid);
	insert('plan', {
		plan_no: res.result,
		plan_name: message.plan_name,
		plan_describe: message.plan_describe,
		plan_type_no: message.plan_type_no,
		plan_type_name: message.plan_type_name,
		plan_cla_attr_no: message.plan_cla_attr_no,
		plan_cla_attr_name: message.plan_cla_attr_name,
		start_date: s,
		end_date: e,
		call_period: message.call_period,
		plan_implement_status: message.plan_implement_status,
		take_effect: message.take_effect,
		plan_location: message.plan_location,
		plan_mark: message.plan_mark,
		plan_cycle: message.plan_cycle,
		request_date: message.request_date,
		plan_list_no: message.plan_list.plan_list_no,
		plan_list_name: message.plan_list.plan_list_name,
		productid
	});

	const r = await exec();

	return {
		data: r
	};
}
